<template>
    <view>
		<view class="container">
			<view class="list-box data-v-0034d6f4">
				<block v-for="(item,index) in categories" :key="index" :data-id="item.id" :data-classtype="item.classType" 
				:data-name="item.name">
					<view class="container jiangqie-page-view"  :data-id="item.id" :data-classtype="item.classType" :data-name="item.name" @click="handlerCategoryClick">
						<image class="emoticon data-v-0034d6f4" mode="aspectFill" :src="item.link?item.link:categorie_cover" ></image>
						<view class="cu-tag bg-blue class-title" style="">{{item.name}}</view>
					</view>
				</block>
			</view>
		</view>
		
        <!--加载框 start-->
        <tui-loading :visible="loading"></tui-loading>
        <!--加载框 end-->
    </view>
</template>

<script>
import tuiLoading from '../../components/loading/loading';
/*
 * Author: 郭少
 * github: https://github.com/guoshao99/wallpaper-applet
 * gitee: https://gitee.com/guoshao99/wallpaper-applet
 * QQ：2549789059
 * 微信：2549789059
 */
const Constants = require('../../utils/constants');

const Api = require('../../utils/api');

const Rest = require('../../utils/rest');

const util = require('../../utils/util.js');

const http = require('../../utils/http.js');

let setinad;
export default {
    components: {
        tuiLoading
    },
    data() {
        return {
            setting: {},
            categories: [],
            categorie_cover: Api.JIANGQIE_CAT_COVER,
            pagead: 1,
            pageadB: 0,
            thisPage: 'categories',

            setAD: {
                banner: '',
                videoid: '',
                gridid: '',
                customAid: '',
                customBid: '',
                customCid: ''
            },

            loading: false,
			default: {
			    background: Api.JIANGQIE_BG_CATEGORY,
			    title: '分类标题，请在后台修改',
			    description: '分类描述，请在后台修改'
			},
        };
    },
    onLoad: function (options) {
        //获取配置
        let that = this; //加载订单列表

        var params = {
            url: '/classification/list',
            method: 'GET',
            data: {},
            callBack: function (res) {
                that.setData({
                    categories: that.categories.concat(res.data)
                });
            }
        };
        http.request(params);
    },
    onShow() {
        var that = this; //分享

        util.getAD(
            that,
            function () {
                that.setInterstitialAd(); //加载插屏广告
            });
    },
    onHide() {
        clearInterval(setinad);
    },
    onShareAppMessage: function () {
        var that = this;
        uni.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        });
        return {
            title: that.shares.share_title,
            imageUrl: that.shares.share_image
        };
    },
    //转发朋友圈
    onShareTimeline: function () {
        var that = this;
        return {
            title: that.shares.share_title,
            imageUrl: that.shares.share_image
        };
    },
    // 收藏
    onAddToFavorites: function () {
        var that = this;
        return {
            title: that.shares.share_title,
            imageUrl: that.shares.share_image
        };
    },
    methods: {
        

        // 获取小程序插屏广告
        setInterstitialAd: function () {
            var that = this;

            if (that.setAD.interstitialid && uni.createInterstitialAd) {
                let interstitialAd = uni.createInterstitialAd({
                    adUnitId: that.setAD.interstitialid
                }); // 监听插屏错误事件

                interstitialAd.onError((err) => {
                    console.error(err);
                }); // 显示广告

                if (interstitialAd) {
                    if (that.setAD.switch_inad == 'yes') {
                        setinad = setInterval(() => {
                            interstitialAd.show().catch((err) => {
                                console.error(err);
                            });
                        }, 2000);
                    } else {
                        setTimeout(() => {
                            interstitialAd.show().catch((err) => {
                                console.error(err);
                            });
                        }, 6000);
                    }
                }
            }
        },

        handlerCategoryClick: function (e) {
            let cat_id = e.currentTarget.dataset.id;
            let cat_name = e.currentTarget.dataset.name;
			let classType = e.currentTarget.dataset.classtype;
            uni.navigateTo({
                url: '/pages/list/list?cat_id=' + cat_id + '&title=' + cat_name + '&classType='+classType
            });
        }
    }
};
</script>
<style>
@import './categories.css';
</style>
